package com.citycloud.ccuap.tc.sync.aspect;

import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.sys.district.IDistrictService;
import com.citycloud.ccuap.commons.sys.entity.SysDeptment;
import com.citycloud.ccuap.commons.sys.entity.SysDistrict;
import com.citycloud.ccuap.commons.sys.entity.SysProject;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncOperation;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncType;
import com.citycloud.ccuap.tc.sync.dto.DepartmentMessageDTO;
import com.citycloud.ccuap.tc.sync.dto.SyncDataDto;
import com.citycloud.ccuap.tc.sync.dto.SysDistrictMessageDTO;
import com.citycloud.ccuap.tc.sync.service.IMessageService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zhangxu
 * @create 2020/7/13
 * Description:
 */
@Aspect
@Component
public class DistrictDeleteAspect {
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IDistrictService districtService;

    @Pointcut("execution(* com.citycloud.ccuap.commons.sys.district.DistrictController.deleteNode())")
    public void delete() {
    }

    @AfterReturning(value = "delete()", returning = "key")
    public void doAddDept(JoinPoint joinPoint, Object key) throws Throwable {
        if (key instanceof JSONData) {
            JSONData jsonData = (JSONData) key;
            Long districtId = (Long) jsonData.getData();
            sendMessage(districtId);
        }
    }

    private void sendMessage(Long districtId) {
        SysDistrictMessageDTO sysDistrictMessageDTO = new SysDistrictMessageDTO();
        sysDistrictMessageDTO.setDistrictId(districtId);
        SyncDataDto<SysDistrictMessageDTO> result = SyncDataDto.of(GlobalSyncType.OBJECT_TYPE_DISTRICT,
                GlobalSyncOperation.DELETED, null, sysDistrictMessageDTO);
        messageService.sendMessageByTopic(result,GlobalSyncType.TOPIC_DISTRICT);
    }

}
